package com.lyc.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.lyc.pojo.FaceModelScore;
import com.lyc.service.FaceSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * 人脸搜索 1：N
 * 将底库人脸头像向量特征注册到内存中，项目重启后注册的人脸将失效
 * 可以将人脸向量特征保存到数据库，项目启动时加载到内存
 */
@Api(value = "人脸搜索API,1:N 搜索", protocols = "http")
@RestController
@RequestMapping(value = "/faceSearch")
public class FaceSearchController {

    @Autowired
    FaceSearchService faceSearchService;

    @ApiOperation(value = "人脸搜索底库注册", notes = "人脸搜索底库注册")
    @PostMapping("/faceRegister")
    public ResponseEntity<List<String>> faceRegister(MultipartFile faceImage) throws IOException {

        Assert.notNull(faceImage, "上传人脸照片不能为空");
        List<String> strings = faceSearchService.registerFace(faceImage);

        return ResponseEntity.ok(strings);
    }

    @ApiOperation(value = "人脸搜索底库注册", notes = "人脸搜索底库注册")
    @DeleteMapping("/face")
    public ResponseEntity<Object> removeFace(String faceId) throws IOException {

        Assert.notNull(faceId, "人脸id不可为空");
        faceSearchService.removeFace(faceId);
        return ResponseEntity.ok("删除成功");
    }

    @ApiOperation(value = "人脸搜索 1:N (只取第一张头像搜索)", notes = "人脸搜索 1:N (只取第一张头像搜索)")
    @PostMapping("/faceSearch")
    public ResponseEntity<List<FaceModelScore>> faceSearch(MultipartFile faceImage) throws IOException {

        Assert.notNull(faceImage, "上传人脸照片不能为空");

        List<FaceModelScore> faceModelScores = faceSearchService.faceSearch(faceImage);
        return ResponseEntity.ok(faceModelScores);
    }

    @ApiOperation(value = "人脸搜索 1:N", notes = "特征值检索")
    @PostMapping("/faceSearch/features")
    public ResponseEntity<List<FaceModelScore>> faceFeatureSearch(@RequestBody String features) throws IOException {
        Float[] data = Arrays.stream(features.split(",")).map(Float::valueOf).toArray(Float[]::new);
        float[] primData = new float[data.length];
        for(int x = 0; x < data.length; x ++) {
            primData[x] = data[x];
        }
        List<FaceModelScore> faceModelScores = faceSearchService.faceSearch(primData);
        return ResponseEntity.ok(faceModelScores);
    }

    @ApiOperation(value = "人脸特征值提取", notes = "获取人脸特征值1024位数组")
    @PostMapping("/faceFeature")
    public ResponseEntity<float[]> faceFeature(MultipartFile faceImage) throws IOException {

        Assert.notNull(faceImage, "上传人脸照片不能为空");
        return ResponseEntity.ok(faceSearchService.faceFeature(faceImage));
    }

}
